function FFMcheckPilot (name, id, alliance, faction) {
    var my = localStorage.getItem ('po_ffm_my_settings');
    var fa = localStorage.getItem ('po_ffm_faction_settings');
    var factionOverrides = localStorage.getItem ('po_ffm_faction_overrides')=="false"?false:true;
    
    var myLvl = 0;
    var faLvl = 0;
    if (my && my.length>0) myLvl = FFMcheckPilotFF (name, id, alliance, faction, my);
    if (fa && fa.length>0) faLvl = FFMcheckPilotFF (name, id, alliance, faction, fa);
    
    if (faLvl == -3 && name != null) {
        if (localStorage.getItem ('po_ffm_report_individual') == 'true')
            FFMreportPilot (name);
    }
    
    if (factionOverrides) {
        if (faLvl == 0) return myLvl;
        if (faLvl > 0 && myLvl > 0) return Math.max(faLvl, myLvl);
        if (faLvl < 0 && myLvl < 0) return Math.min(faLvl, myLvl);
        return faLvl;
    }
    if (myLvl == 0) return faLvl;
    if (faLvl > 0 && myLvl > 0) return Math.max(faLvl, myLvl);
    if (faLvl < 0 && myLvl < 0) return Math.min(faLvl, myLvl);
    return myLvl;
}

function FFMcheckPilotFF (name, id, alliance, faction, ff) {
    var ffs = ff.split("]");
    var friend = ffs[0];
    var foe = ffs[1];
    
    // Check ID if provided. If not, check name
    if (id) {
        if (friend.indexOf ("#"+id+";") != -1) return 3;
        if (foe.indexOf ("#"+id+";") != -1) return -3;
    } else if (name) {
        if (friend.indexOf (name+"#") != -1) return 3;
        if (foe.indexOf (name+"#") != -1) return -3;
    }
    
    // Check Alliance
    if (alliance) {
        if (friend.indexOf (alliance+";") != -1) return 2;
        if (foe.indexOf (alliance+";") != -1) return -2;
    }
    
    // Check Faction
    if (faction) {
        if (friend.indexOf (faction+";") != -1) return 1;
        if (foe.indexOf (faction+";") != -1) return -1;
    }
    return 0;
}

function FFMreportPilot (pilotName) {
    var coords = POgetCoords ();
    var sector = POgetSector ();
    var key = pilotName+"@"+sector+"@"+coords+";";
    var queue = localStorage.getItem ("ffm_sightings_queue");
    localStorage.setItem ("ffm_sightings_queue", queue+key);
}

function FFMcheckOtherPilots () {
    if (POisPartialRefresh()) {
        var ops = window.shipsTab;
    } else {
        var ops = document.getElementById ("otherships_content");
    }
    var firstPilot = null;
    var firstAlly = null;
    var firstFact = null;
    var otherShips = ops.childNodes;
    var sort = new Array ();
    for (var i=0; i<otherShips.length; i++) {
        var os = otherShips[i];
        sort.push(os);
        var ship = os.firstChild.firstChild;
        var fact = FFMgetPilotFaction (ship);
        var ally = FFMgetPilotAlly (ship);
        var id = FFMgetPilotId (ship);
        var name = FFMgetPilotName (ship);
        var ff = FFMcheckPilot (name, id, ally, fact)
        var color = null;
        var style = null;
        if (ff < 0) {
            color = "red";
        } else if (ff > 0) {
            color = "lime";
        } else color = "gray";
        if (Math.abs (ff) == 1) style="dashed";
        else if (Math.abs (ff) == 2) style="dotted";
        else style="solid";
        os.setAttribute ("style", "border-left:2px "+style+" "+color+";");
        os.setAttribute ("po_ffm_ff", ff);
        os.setAttribute ("po_ffm_id", id);
        os.setAttribute ("po_ffm_faction", fact==null?"":fact);
        os.setAttribute ("po_ffm_ally", ally==null?"":ally);
    }
    if (localStorage.getItem("po_ffm_sort_pilots") == 'true') {
        FFMSortPilots (sort);
        ops.innerHTML="";
        for (var i=0; i<sort.length; i++){
            ops.appendChild (sort[i]);
        }
    }
}

var sortSettings;
function FFMSortPilots (otherShips) {
    var my = localStorage.getItem ('po_ffm_my_settings');
    var fa = localStorage.getItem ('po_ffm_faction_settings');
    var factionOverrides = localStorage.getItem ('po_ffm_faction_overrides')=="false"?false:true;
    sortSettings = factionOverrides==true?fa+";"+my:my+";"+fa;
    
    otherShips.sort (FFMcomparePilots);
}

function FFMcomparePilots (first, second) {
    // More negative number = more up the sip should be in the list
    var ff1 = Number(first.getAttribute("po_ffm_ff"));
    var ff2 = Number(second.getAttribute("po_ffm_ff"));
    if (ff1 != ff2) {
        if (ff1 == 0) return -1;
        if (ff2 == 0) return 1;
        if (ff1>0 && ff2>0)
            if (ff1 > ff2) return -1;
            else return 1;
        if (ff1 < ff2) return -1;
        else return 1;
    }
    var indFirst = sortSettings.indexOf ("#"+first.getAttribute("po_ffm_id")+";");
    var indSec = sortSettings.indexOf ("#"+second.getAttribute("po_ffm_id")+";");
    
    if (indFirst==-1 && indSec==-1) {
        // None of the pilots is listed directly. Let's check their alliances and factions
        //var ff1 = Number(first.getAttribute ("po_ffm_ally");
        return 0;
    }
    if (indFirst==-1) return -1;
    if (indSec==-1) return 1;
    if (indFirst < indSec) return -1;
    return 1;
}

function FFMgetPilotFaction (ship) {
    var factions = ship.getElementsByTagName ("img");
    for (var i=0; i<factions.length; i++) {
        var faction = factions[i];
        var file = faction.getAttribute("src").split ("/");
        file = file[file.length-1];
        if (file.indexOf("fed")!=-1) return "Federation";
        if (file.indexOf("uni")!=-1) return "Union";
        if (file.indexOf("emp")!=-1) return "Empire";
    }
    return "- No faction alignment";
}

function FFMgetPilotAlly (ship) {
    var fonts = ship.getElementsByTagName ("font");
    for (var i=0; i<fonts.length; i++) {
        var cell = fonts[i];
        if (cell.getAttribute("color")=="#A1A1AF" && cell.parentNode.tagName.toLowerCase()=="a" && cell.parentNode.getAttribute("href").indexOf ("alliance.php") != -1) {
            return cell.textContent;
        }
    }
    return "- No alliance participation";
}

function FFMgetPilotName (ship) {
    var links = ship.getElementsByTagName ("a");
    for (var i=0; i<links.length; i++) {
        var link = links[i];
        var href = link.getAttribute ("href");
        if (href.indexOf ("scanId(") != -1 || href.indexOf ("main.php?scan_details") != -1) {
            return link.textContent;
        }
    }
    return null;
}

function FFMgetPilotId (ship) {
    var links = ship.getElementsByTagName ("a");
    for (var i=0; i<links.length; i++) {
        var link = links[i];
        var href = link.getAttribute ("href");
        if (href.indexOf ("scanId(") != -1) {
            return href.split("scanId(")[1].split(",")[0];
        }
        if (href.indexOf ("main.php?scan_details") != -1){
            return href.split("details=")[1].split("&")[0];
        }
    }
    return null;
}

function FFMhijackUpdateShips () {
    var local = window.updateShips;
    if (local != undefined) {
        window.updateShips = function(cmd) {
            local(cmd);
            FFMcheckOtherPilots ();
        }
    }
}

window.onload = setTimeout(function () {
    FFMcheckOtherPilots ();
    FFMhijackUpdateShips ();
}, 10);
